package com.mlz;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.mlz.domain.po.User;
import com.mlz.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
public class UserMapperTest02 {

    @Autowired
    private UserMapper userMapper;

    //需求1： 查询名字中带有o的，并且存款是大于等于800的人
    @Test
    public void test01() {
        //1.构建查询条件：where username like %o% and balance >= 800
        // 新建查询对象
        QueryWrapper<User> wrapper = new QueryWrapper<User>()
                .select("id","username","balance")// 添加查询条件
                .like("username","o")
                .ge("balance",800);;
        //查询数据
        List<User> users = userMapper.selectList(wrapper);
        System.out.println(users);

    }

    //需求2: 修改用户名jack的钱为5000
    @Test
    public void test02() {
        //条件构建SQL: where username = Jack
        QueryWrapper<User> wrapper = new QueryWrapper<User>()
                .eq("username", "Jack");

        //更新数据
        User user = new User();
        user.setBalance(5000);
        userMapper.update(user, wrapper);
    }

    // 需求: 更新id为1 3 5的用户金额,都扣除100元
    @Test
    public void test03() {
        //update user set balance = balance - 100 where id in(1,3,5);

        List<Long> ids = List.of(1L, 3L, 5L);

        //生成条件对象,并且封装修改条件
        UpdateWrapper<User> wrapper = new UpdateWrapper<User>()
                .setSql("balance = balance - 100")//set balance = balance - 100
                .in("id", ids);

        //进行更新数据
        userMapper.update(null,wrapper);
    }


    //lambda方式优化以上的需求
    @Test
    public void test04() {
        //SQL:where username like %o% and balance >= 800
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<User> lqw = wrapper.lambda()
                .select(User::getId, User::getUsername, User::getBalance)
                .like(User::getUsername, "o")
                .ge(User::getBalance, 800);

        //查询
        List<User> users = userMapper.selectList(lqw);
        users.forEach(System.out::println);

    }


    // 自定义SQL + 组装SQL 基础用法
    // update user set balance = balance - 100 where id in(1,3,5);
//    @Test
//    public void test05() {
//        // ids集合
//        List<Long> ids = List.of(1L, 3L, 5L);
//        // 创建条件对象
//        QueryWrapper<User> wrapper = new QueryWrapper<User>().in("id",ids);
//
//        // 在userMapper中自定义方法,直接传递wrapper
//        userMapper.decuctBalanceByIds(100,wrapper);
//    }

    // <!--需求:查询出所有收获地址在浙江并且是在用户id在1,3,5-->
//    @Test
//    void test06() {
//        // 1.准备自定义查询条件
//        QueryWrapper<User> wrapper = new QueryWrapper<User>()
//                .in("u.id", List.of(1L, 2L, 4L))
//                .eq("a.city", "北京");
//        String segment = wrapper.getCustomSqlSegment();
//        System.out.println(segment);
//
//        // 2.调用mapper的自定义方法
//        List<User> users = userMapper.queryUserByWrapper(wrapper);
//
//        users.forEach(System.out::println);
//    }



    //测试Db的功能方法
    @Test
    public void test07(){
        User user = Db.getById(1L, User.class);
        System.out.println(user);
    }

    @Test
    public void test08(){
        List<User> list = Db.lambdaQuery(User.class)
                .like(User::getUsername, "o")
                .ge(User::getBalance, 800)
                .list();

        System.out.println(list);
    }

    @Test
    public void test09(){
        // 修改
        LambdaUpdateChainWrapper<User> wrapper =
                Db.lambdaUpdate(User.class)
                .set(User::getBalance, 1000)
                .eq(User::getUsername, "Jack");
        System.out.println(wrapper);
    }




}
